package com.java复习.线程;

public class Demo3 {
    public static void main(String[] args) {
        Station station = new Station();
        Thread t1 = new Thread(station);
        t1.setName("线程一");
        Thread t2 = new Thread(station);
        t2.setName("线程二");
        Thread t3 = new Thread(station);
        t3.setName("线程三");

        t1.start();
        t2.start();
        t3.start();

    }
}

// 线程锁：被线程锁包裹的代码，一次只能由一个线程来执行  -----  Java中Object类自带线程锁

class Station implements Runnable{
    static int total = 1000;

    @Override
    public synchronized void run() {  // 加synchronized 解决线程安全的问题
        while (total>0){


                total--;
                if (total>0){
                    System.out.println(Thread.currentThread().getName()+"售出一张票，余票"+total+"张");
                }


        }
    }
}
